﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LanHomepage.ViewModels;
using BusinessLogic;
using Models;

namespace LanHomepage.Controllers
{
    public class MainController : BaseController
    {
        private readonly IDatabaseHandler _databaseHandler;

        public MainController(IDatabaseHandler databaseHandler)
            :base(databaseHandler)
        {
            _databaseHandler = databaseHandler;
        }

        public ActionResult Mainpage(MainViewModel model)
        {
            model.Images = _databaseHandler.GetImagelist();
            model.NewsPosts = _databaseHandler.GetNewsPosts();

            return View(model);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult NewsUpload(string headline, string news_text)
        {
            if (headline != "" && news_text != "") { 
                _databaseHandler.AddNews(headline, news_text);

            }
            return RedirectToAction("Mainpage", "Main");
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public bool EditNewsPost(string _id, string _headline, string _news_text)
        {
            return _databaseHandler.EditNews(_id, _headline, _news_text);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public bool DeleteNewsPost(string _id) 
        {
            return _databaseHandler.DeleteNews(_id);
        }
    }
}
